$(function(){
    layui.use("layer", function () {
        var layer = layui.layer;
    });

    var page=1;
    getmore(page);
    

    $("#loadmore").click(function(){
        page++;
        getmore(page);
    })

    var isAdmin=""
    if($(".shuserid")){
        isAdmin=$(".shuserid").attr("data-isadmin");
    }
    


    
    function getmore(index){
        $.ajax({
            url:"/log/personalspace",
            type:"post",
            success:function(msg){
                var str="";
                var limit=6;
                var count=Math.min(index*limit,msg.art.length);
                // 设置用户发布日志的数量
                console.log(msg.art.length,$(".mount"));
                $(".mount").html(msg.art.length);
                
                if(isAdmin=="true"){
                    for(var i=0;i<count;i++){
                        str+=`
                        <div class="space-box clearfix">
                                <div class="s-photo">
                                    <div>
                                        <img src="/photo/${msg.art[i].author.photo}" alt="">
                                    </div>
                                </div>
                                <div class="s-chat">
                                    <div class="chat-top">
                                            <span class="chat-user">${msg.art[i].author.userName}：</span>
                                            <span class="chat-info">
                                                ${msg.art[i].content}
                                            </span>
                                    </div>
                                    <div class="chat-middle">
                                        <span>${new Date(msg.art[i].date).toLocaleString('chinese', { hour12: false })}</span>
                                    </div>
                                    <div class="chat-bottom">
                                        <textarea name="" class="input" cols="30" rows="10" placeholder="我来评论" maxlength="100"></textarea>
                                        <button class="view" data-id=${msg.art[i]._id}>查看</button>
                                        <button class="send" data-id=${msg.art[i]._id}>评论</button>
                                        <button class="s-delete" data-id=${msg.art[i]._id}>删除</button>
                                    </div>
                                    <div class="p-comment ">
                                    
                                    </div>
                                </div>
                                <div class="clear"></div>
                        </div>
                        `
                    }
                }else{
                    for(var i=0;i<count;i++){
                        str+=`
                        <div class="space-box clearfix">
                                <div class="s-photo">
                                    <div>
                                        <img src="/photo/${msg.art[i].author.photo}" alt="">
                                    </div>
                                </div>
                                <div class="s-chat">
                                    <div class="chat-top">
                                            <span class="chat-user">${msg.art[i].author.userName}：</span>
                                            <span class="chat-info">
                                                ${msg.art[i].content}
                                            </span>
                                    </div>
                                    <div class="chat-middle">
                                        <span>${new Date(msg.art[i].date).toLocaleString('chinese', { hour12: false })}</span>
                                    </div>
                                    <div class="chat-bottom">
                                        <textarea name="" class="input" cols="30" rows="10" placeholder="我来评论" maxlength="100"></textarea>
                                        <button class="view" data-id=${msg.art[i]._id}>查看</button>
                                        <button class="send" data-id=${msg.art[i]._id}>评论</button>
                                    </div>
                                    <div class="p-comment ">
                                    
                                    </div>
                                </div>
                                <div class="clear"></div>
                        </div>
                        `
                    }
                }

                $("#log-box").html(str);
                // 删除日志
                $(".s-delete").click(function(){
                    $.ajax({
                        url:"/log/delete",
                        method:"get",
                        data:{
                            id:$(this).attr("data-id")
                        },
                        success:function(msg){
                            if(msg.code){
                                layer.msg(
                                    msg.message,
                                    {
                                        icon: 1,
                                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                    },
                                    function () {

                                        getmore(1);
                                    }
                                );
                            }else{
                                layer.msg(
                                    msg.message,
                                    {
                                        icon: 5,
                                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                    },
                                    function () {
                                        
                                    }
                                );
                            }
                        }
                    })

                })
                var flag=false;
                // 评论日志
                $(".send").click(function(){
                    var ifsend=$("#s-right").attr("data-iflogin")
                    console.log(ifsend);
                    if(ifsend!="true"){
                        layer.msg(
                            "登陆后可评论",
                            {
                                icon: 5,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            },
                            function () {
                                
                            }
                        );
                        return;
                    }
                    var text=$(this).prevAll()[1]
                    var comment=text.value;
                    if(comment==""){
                        layer.msg(
                            "评论不能为空",
                            {
                                icon: 5,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            },
                            function () {
                                
                            }
                        );
                        return;
                    }
                    var id=$(this).attr("data-id");
                    var combox=$(this).parents().next()[0];
                    $.ajax({
                        url:"/logcom/published",
                        method:"get",
                        data:{
                            user:$(".shuserid").html(),
                            log:$(this).attr("data-id"),
                            content:comment,
                            date:new Date()
                        },
                        success:function(msg){
                            if(msg.code){
                                layer.msg(
                                    msg.message,
                                    {
                                        icon: 1,
                                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                    },
                                    function () {
                                        text.value="";
                                        console.log(id,combox);
                                        flag=true;
                                        getComment(id,combox);
                                    }
                                );
                            }else{
                                layer.msg(
                                    msg.message,
                                    {
                                        icon: 5,
                                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                    },
                                    function () {
                                        
                                    }
                                );
                            }
                        }
                    })
                })

                // 获取日志评论
                
                $(".view").click(function(){
                    var id=$(this).attr("data-id");
                    var combox=$(this).parents().next()[0];
                    getComment(id,combox);
                    flag=!flag;
                })
                
                
                function getComment(id,combox){
                    var str=""
                    $.ajax({
                        url:"/logcom/toobtain",
                        method:"get",
                        data:{
                            id
                        },
                        success:function(msg){
                            msg.result.forEach(item => {
                                str+=`
                                    <div class="pc">
                                        <span class="pc-left">
                                            <img src="/photo/${item.user.photo}" alt="">
                                        </span>
                                        <span class="pc-author">${item.user.userName}：</span>
                                        <div class="pc-content">${item.content}</div>
                                        <div class="pc-time">${new Date(item.date).toLocaleString('chinese', { hour12: false })}</div>
                                    </div>
                                `
                            });
                            if(str==""){
                                layer.msg(
                                    "暂无评论",
                                    {
                                        icon: 5,
                                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                    },
                                    function () {
                                        
                                    }
                                );
                            }else{
                                $(combox).html(str);
                                if(flag){
                                    $(combox).slideDown()
                                }else{
                                    $(combox).slideUp();
                                }
                                
                            }
                            
                        }
                    })
                }
            }
        })
    }

    // 表情
    $.Lemoji({
        emojiInput: '#input',
        emojiBtn: '#btn',
        position: 'RIGHT',
        length: 8,
        emojis: {
            qq: {path: '/static/images/qq/', code: ':', name: 'QQ表情'},
            tieba: {path: '/static/images/tieba', code: ';', name: "贴吧表情"},
            emoji: {path: '/static/images/emoji', code: ',', name: 'Emoji表情'}
        }
    });
            
    // 表情解析
    $('#send').click(function () {
        var content = $('#input').val();
        content = $.emojiParse({
            content: content,
            emojis: [
                {
                    type: 'qq', path: '/static/images/qq/', code: ':'
                }, 
                {
                    path: '/static/images/tieba/',
                    code: ';',
                    type: 'tieba'
                }, 
                {
                    path: '/static/images/emoji/', code: ',', type: 'emoji'
                }
            ]
        });
        // $('#test').html(content);
        $.ajax({
            url:"/log/published",
            method:"post",
            data:{
                content,
                date:new Date()
            },
            success:function(msg){
                if(msg.code==1){
                    layer.msg(
                        msg.message,
                        {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        },
                        function () {
                            $("#input").val("");
                            window.location.reload();
                        }
                    );
                }else{
                    layer.msg(
                        msg.message,
                        {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        },
                        function () {
                            
                        }
                    );
                }
            }
        })
    });

   
})